package com.cool.reggie.modules.setmeal.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cool.reggie.modules.setmeal.dto.SetmealDto;
import com.cool.reggie.modules.setmeal.entity.SetmealEntity;
import com.cool.reggie.modules.setmeal.service.SetmealService;
import com.cool.reggie.modules.setmealdish.service.SetmealDishService;
import com.cool.reggie.utils.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author 1950735817@qq.com
 * @since 2022-07-06
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    public SetmealController(SetmealService setmealService, SetmealDishService setmealDishService) {
        this.setmealService = setmealService;
        this.setmealDishService = setmealDishService;
    }

    private SetmealService setmealService;
    private SetmealDishService setmealDishService;

    @GetMapping("/page")
    public ResultData getPage(int page, int pageSize, String name) {

        Page<SetmealDto> pageResult = setmealService.myPageQuery(page, pageSize, name);

        return ResultData.ok(pageResult);
    }

    @PostMapping
    public ResultData saveSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithSetmealDto(setmealDto);
        return ResultData.ok();
    }

    @GetMapping("{id}")
    public ResultData queryInfo(@PathVariable("id") Long id) {
        SetmealDto setmealDto = setmealService.getByIdWithDishes(id);
        return ResultData.ok(setmealDto);
    }

    @PutMapping
    public ResultData saveInfo(@RequestBody SetmealDto setmealDto) {
        setmealService.saveByIdWithDishes(setmealDto);
        return ResultData.ok();
    }

    @DeleteMapping
    public ResultData deleteInfo(@RequestParam("ids") List<Long> ids) {
        setmealService.deleteByIdWithSetmeal(ids);
        return ResultData.ok();
    }

    @PostMapping("/status/0")
    public ResultData stopSetmeal(@RequestParam("ids") List<Long> ids) {
        setmealService.stopByIdWithSetmeal(ids);
        return ResultData.ok();
    }


    @GetMapping("/list")
    public ResultData getList(SetmealEntity setmealEntity) {

        LambdaQueryWrapper<SetmealEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(setmealEntity.getCategoryId().toString()), SetmealEntity::getCategoryId, setmealEntity.getCategoryId());
        queryWrapper.eq(StringUtils.isNotBlank(setmealEntity.getStatus().toString()), SetmealEntity::getStatus, setmealEntity.getStatus());
        List<SetmealEntity> list = setmealService.list(queryWrapper);

        return ResultData.ok(list);
    }


    @GetMapping("/dish/{id}")
    public ResultData getList(@PathVariable("id") Long id) {

        return ResultData.ok();
    }

}
